package com.qh.yyxt.service.Impl;

import com.qh.yyxt.pojo.entity.thing.Thing;
import com.qh.yyxt.pojo.entity.thing.ThingSize;
import com.qh.yyxt.mapper.ThingMapper;
import com.qh.yyxt.mapper.ThingSizeMapper;
import com.qh.yyxt.pojo.ThingSizeDTO;
import com.qh.yyxt.service.ThingSizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ThingSizeServiceImpl implements ThingSizeService {

    @Autowired
    private ThingSizeMapper thingSizeMapper;
    @Autowired
    private ThingMapper thingMapper;
    @Override
    public int addThingSize(ThingSize thingSize) {
        if (thingSize != null)
            return thingSizeMapper.add(thingSize);
        return 0;
    }

    @Override
    public int updateNum(Integer id, Integer num, Integer renum) {
        if(id!=null)
        {
            return thingSizeMapper.updateNum(id,num,renum);
        }else{
            return 0;
        }
    }


    @Override
    public int delete(Integer id) {
        if (id != null)
            return thingSizeMapper.delete(id);
        return 0;
    }

    @Override
    public int update(ThingSize thingSize) {
        if (thingSize != null)
            return thingSizeMapper.update(thingSize);
        return 0;
    }

    @Override
    public Boolean judgeNum(Integer sid, Integer num)
    {
        if(sid!=null&&num!=null)
        {
            if(num<=thingSizeMapper.findRenumById(sid))
            {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<ThingSizeDTO> findByTid(Integer tid)
    {
        if(tid!=null)
        {
            List<ThingSize>thingSizeList=thingSizeMapper.findByTid(tid);
            List<ThingSizeDTO>thingSizeDTOList=new ArrayList<>();
            if(thingSizeList.isEmpty())
            {
                return thingSizeDTOList;
            }
            Thing thing=thingMapper.findById(tid);
            for (ThingSize thingSize:thingSizeList)
            {
                ThingSizeDTO thingSizeDTO=new ThingSizeDTO();
                thingSizeDTO.setThingSize(thing,thingSize);
            }
        }
        return null;
    }

    @Override
    public ThingSizeDTO findById(Integer id)
    {
        if(id!=null)
        {
            ThingSizeDTO thingSizeDTO=new ThingSizeDTO();
            ThingSize thingSize=thingSizeMapper.findById(id);
            if(thingSize!=null)
            {
                thingSizeDTO.setThingSize(thingMapper.findById(thingSize.getTid()),thingSize);
            }
        }
        return null;
    }
}
